
var curDocName = "";

//function loadDocument(docName, username) {
function loadDocument(docName, username) {

	$("#export_spinner").show();
	$("input[id*='export']").attr("disabled", "disabled");
	$.ajax({
		type: "POST",
		url: "cgi/load.php",
		//url: "load.py",
		data: "name="+docName+"&username="+username, //$("#export_username").val(),
		success: function(jsonStr) {
			//alert("Data loaded.");
			//alert(jsonStr);
			//cprint(jsonStr);

			curDocName = docName;

			// reset id numbers for consistency
			CanvasObject.nextId = 0;
			
			
			var jsonObj = JSON.parse(jsonStr);
			cprint(jsonObj.json);
			
			theCanvas().setBaseObject( CanvasObject.buildFromJSON( 
				//JSON.parse( jsonStr )
				jsonObj.json
			));
			
			theLayoutDocument().filename = docName;
			theLayoutDocument().json = jsonObj.json;
			theLayoutDocument().id = jsonObj.id;
			theLayoutDocument().name = jsonObj.name;
			theLayoutDocument().category = jsonObj.username;
			theLayoutDocument().author = jsonObj.author;
			theLayoutDocument().title = jsonObj.title;
			theLayoutDocument().description = jsonObj.description;
			theLayoutDocument().js = jsonObj.js;
			theLayoutDocument().cssClasses = jsonObj.cssClasses;
			
			$("#export_username").val(jsonObj.username);
			$("#export_author").val(jsonObj.author);
			$("#export_title").val(jsonObj.title);
			$("#export_description").val(jsonObj.description);
			
			$("#textarea_editCssClasses").val(jsonObj.cssClasses);
			$("#textarea_editJavascript").val(jsonObj.js);
			
			$("#export_spinner").hide();
			$("input[id*='export']").removeAttr("disabled");
		}
	
	});

}

function insertDocument(docName, username) {
	
	$("#export_spinner").show();
	$("input[id*='export']").attr("disabled", "disabled");
	$.ajax({
		type: "POST",
		url: "cgi/load.php",
		data: "name="+docName+"&username="+username,
		success: function(jsonStr) {
			//alert("Data loaded.");
			
			//alert("Data loaded.");
			var 
			jsonStr = jsonStr.replace("\n", " ");
			jsonStr = jsonStr.replace("\r", " ");
			
			alert(jsonStr);
			cprint(jsonStr);


			var jsonObj = JSON.parse(jsonStr);
			
			theCanvas().getBaseObject().addObject( 
				CanvasObject.buildFromJSON( jsonObj.json )
			);
			
			$("#export_spinner").hide();
			$("input[id*='export']").removeAttr("disabled");
		}
	
	});
	
}

function saveDocument(docName) {

	cprint("Save!");
	cprint(JSON.stringify(theCanvas().getBaseObject()));
	
	$("#export_spinner").show();
	$("input[id*='export']").attr("disabled", "disabled");
	$.ajax({
		type: "POST",
		url: "cgi/save.php",
		data: "name="+docName+"&json=" + JSON.stringify(theCanvas().getBaseObject())+
			"&username="+$("#document_category").val()+
			"&author="+$("#document_author").val()+
			"&title="+$("#document_title").val()+
			"&description="+$("#document_description").val() +
			"&js="+theLayoutDocument().js +
			"&cssClasses="+theLayoutDocument().cssClasses,
		success: function(msg){
			$("#export_spinner").hide();
			$("input[id*='export']").removeAttr("disabled");
			
			alert( "Data Saved: " + msg );
			cprint(msg, "#000000");
			curDocName = docName;
			$("#exportdoclist").load("cgi/doclist.php", 
				{ username: $("#export_username").val() } );
		}
	});
	
	theLayoutDocument().filename = docName;


}

function deleteDocument(docName) {

	$("#export_spinner").show();
	$("input[id*='export']").attr("disabled", "disabled");
	$.ajax({
		type: "POST",
		url: "cgi/delete.php",
		data: "name="+docName+
			"&username="+$("#export_username").val(),
		success: function(msg) {
			curDocName = "";
			$("#export_spinner").hide();
			$("input[id*='export']").removeAttr("disabled");
			alert( "Document deleted: " + msg );
			$("#exportdoclist").load("cgi/doclist.php", 
				{ username: $("input[id=='export_username']").val() } );
		}
	});


}